package com.supplychainapi.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * Created by wangziqi on 2017/9/20.
 */
public class JsonUtils {
    public static final Gson gson = (new GsonBuilder()).serializeNulls().create();
    public static ObjectMapper objectMapper = new ObjectMapper();

    public JsonUtils() {
    }

    public static String toJson(Object o) {
        return gson.toJson(o);
    }

    public static <T> T fromJson(String json, Class<T> classOfT) {
        return gson.fromJson(json, classOfT);
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        return gson.fromJson(json, typeOfT);
    }

    public static JsonNode getJsonNode(String detailJson) throws IOException {
        return objectMapper.readTree(detailJson);
    }
}
